GBML manual / Grids

grid_width

How many columns target has - the first number grid_create(width, height, fill) was given, so the x in target[# x, y] runs from 0 up to one less than this. It is worked out when the game is built, so it costs nothing while the game runs.

Syntax

grid_width(target) -> int

Arguments

NameType
targetgrid

Returns

int

Description

Tells you how many columns target, a grid, has: the first number grid_create was given. In target[# x, y], x counts across, so it runs from 0 up to one less than grid_width(target).

A grid made with grid_create(8, 6, 0) is 8 columns wide, so grid_width gives 8. Its other size, how many rows it has, is grid_height.

Why a grid has two sizes

A grid is not one run of places the way an array is. It is a block, and a block has an across and a down. That is why grid_create(width, height, fill) takes two numbers, and why there are two functions to ask for them, grid_width and grid_height, named for the two numbers you gave it.

Going across every column

var board = grid_create(8, 6, 0);
var across = grid_width(board);    // 8
for (var x = 0; x < grid_width(board); x++) {
    board[# x, 0] = 1;             // the whole top row
}

It costs nothing while the game runs

grid_width is not worked out while the game runs. The number is written into the game when it is built, from the size the grid was declared with: grid_create(8, 6, 0), a /// @capacity 8x6 tag, or grid<int, 8, 6>. A grid made any of those ways knows its width.

A copy keeps it: grid_copy makes a grid with the same width and height.

When a grid has no width

A grid only knows its width where it was declared. Three grids have none to give, and grid_width on one is refused when the project is checked, the same way [# x, y] is:

So inside a script, a grid parameter can be handed on to other code, but it cannot be asked its width or height, and it cannot be read with board[# x, y] either. Do the work on the grid in the object that made it, and give the script plain numbers - the width and height, or one cell's value - instead of the grid.

grid_width takes a grid and nothing else. Giving it an array or a list is refused.

On the GBA

A plain number written into the C++ at build time: the width the grid was declared with (`grid_create(8, 6, 0)`, `/// @capacity 8x6` or `grid<int, 8, 6>`) - the same number [# x, y] multiplies the row by. No call and no memory: a fixed-size vector of W*H cells does not store its own width, only its total cell count, which is why this is read from the declaration. For the same reason a grid parameter of a script has no width to give - the caller's width never reaches the script, so pass it in as a second parameter.

See also: grid_heightgrid_creategrid_fill_regiongrid_copyGridsarray_lengthData containersgrid_sortgrid_shuffle_rowsgrid_shuffle_columns